package com.ocom.payhandle.fun.impl;

import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.entity.card.EntityXfRecordProcessExxx;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payhandle.fun.ShoppingCartPayDiscountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Slf4j
@Service
public class ShoppingCartPayDiscountServiceImpl implements ShoppingCartPayDiscountService {


    @Override
    public Result discountTimes(Long companyId, EntityXfRecord entityXfRecord, List<EntityXfRecordProcessExxx> processExxxList) {

//        JoinLambdaWrapper<EntityXfRecordProcessExxx> queryWrapper = new JoinLambdaWrapper<>(EntityXfRecordProcessExxx.class);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPId, entityXfRecord.getPId());
//        queryWrapper.eq(EntityXfRecordProcessExxx::getComId, entityXfRecord.getComId());
////        queryWrapper.eq("cons_state",1);
//        queryWrapper.in(EntityXfRecordProcessExxx::getConsState, 1, 3, 5);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPayState, 1);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getIfRefund, 0);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getConsType, entityXfRecord.getConsType());

        Stream<EntityXfRecordProcessExxx> exxxes2 = processExxxList.stream();

        if (!CommonUtil.isNull(entityXfRecord.getReserved1()) && entityXfRecord.getReserved1().equals("selectMerchTrue")) {
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMerchId, entityXfRecord.getMerchId());
            exxxes2 = exxxes2.filter(o -> o.getMerchId().equals(entityXfRecord.getMerchId()));
        }

        if (!CommonUtil.isNull(entityXfRecord.getReserved2())) {
//            queryWrapper.apply("find_in_set(  xf_record_process.meal_no , \"" + entityXfRecord.getReserved2() + "\" )  and xf_record_process.meal_no IS NOT NULL ");
            List<String> meals = Arrays.asList(entityXfRecord.getReserved2().split(","));
            exxxes2.filter(o-> meals.contains(o.getMealNo()));
        }

        String strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);

        if (entityXfRecord.getMealNo().equals("500")) {
            //餐段按天
            strDate = strDate.substring(0, 10);
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMealDate, strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        } else if (entityXfRecord.getMealNo().equals("1000")) {
            //按月查询
            strDate = strDate.substring(0, 7).replace("-", "");
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMonthValue, strDate);
            String finalStrDate = strDate;
            exxxes2 = exxxes2.filter(o -> o.getMonthValue().equals(finalStrDate));
        } else {
            if (CommonUtil.isNull(entityXfRecord.getReserved2())) {
//                queryWrapper.eq(EntityXfRecordProcessExxx::getMealNo, entityXfRecord.getMealNo());
                exxxes2 = exxxes2.filter(o -> o.getMealNo().equals(entityXfRecord.getMealNo()));
            }
            strDate = strDate.substring(0, 10);
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMealDate, strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        }

//        queryWrapper.notDefaultSelectAll().leftJoin(EntityXfRecordProcessExtend.class, EntityXfRecordProcessExtend::getOrderId, EntityXfRecordProcessExxx::getId)
//                .selectAs((cb) -> {
//                    cb.add(" count(*) ","times",false); // 这个为false就是代表不是字符串，会原样查询 在1.3.1版本后支持
//                }).apply("( xf_record_process_extend.cancel_state = 0 or  xf_record_process_extend.cancel_state is null  ) ").end().joinList(TimesAmountVoExxx.class);

        exxxes2 = exxxes2.filter(o -> o.getCancelState() == null || o.getCancelState() == 0);

        Map<String, Object> map = new HashMap<>();
        map.put("times", exxxes2.count());
        return ResultUtil.success(map);
    }

    @Override
    public Result discountTimesManJian(Long companyId, EntityXfRecord entityXfRecord, List<EntityXfRecordProcessExxx> processExxxList) {

        Stream<EntityXfRecordProcessExxx> exxxes2 = processExxxList.stream();

//        JoinLambdaWrapper<EntityXfRecordProcessExxx> queryWrapper = new JoinLambdaWrapper<>(EntityXfRecordProcessExxx.class);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPId, entityXfRecord.getPId());
//        queryWrapper.eq(EntityXfRecordProcessExxx::getComId, entityXfRecord.getComId());
//        queryWrapper.in(EntityXfRecordProcessExxx::getConsState, 1, 3, 5);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPayState, 1);
//        queryWrapper.gt(EntityXfRecordProcessExxx::getDiscount, 0);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getIfRefund, 0);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getConsType, entityXfRecord.getConsType());

        exxxes2 = exxxes2.filter(o -> o.getDiscount() > 0l);


        if (!CommonUtil.isNull(entityXfRecord.getReserved1()) && entityXfRecord.getReserved1().equals("selectMerchTrue")) {
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMerchId, entityXfRecord.getMerchId());
            exxxes2 = exxxes2.filter(o -> o.getMerchId().equals(entityXfRecord.getMerchId()));
        }
        if (!CommonUtil.isNull(entityXfRecord.getReserved2())) {
//            queryWrapper.apply("find_in_set(  xf_record_process.meal_no , \"" + entityXfRecord.getReserved2() + "\" )  and xf_record_process.meal_no IS NOT NULL ");
            List<String> meals = Arrays.asList(entityXfRecord.getReserved2().split(","));
            exxxes2.filter(o-> meals.contains(o.getMealNo()));
        }

        String strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);

        //餐段按天
        strDate = strDate.substring(0, 10);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getMealDate, strDate);
        String finalStrDate1 = strDate;
        exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));

//        queryWrapper.groupBy(EntityXfRecordProcessExxx::getDiscount);
//
//        queryWrapper.notDefaultSelectAll().select(EntityXfRecordProcessExxx::getDiscount).leftJoin(EntityXfRecordProcessExtend.class, EntityXfRecordProcessExtend::getOrderId, EntityXfRecordProcessExxx::getId)
//                .select(EntityXfRecordProcessExtend::getCancelState).selectAs((cb) -> {
//                    cb.add(" count(*) ","times",false); // 这个为false就是代表不是字符串，会原样查询 在1.3.1版本后支持
//                }).apply("( xf_record_process_extend.cancel_state = 0 or  xf_record_process_extend.cancel_state is null  ) ").end();
//
//        return this.joinList(queryWrapper, TimesAmountVoExxx.class);
        exxxes2 = exxxes2.filter(o -> o.getCancelState() == null || o.getCancelState() == 0);

        Map<Long, Long> merchPayCountMap = exxxes2.collect(Collectors.groupingBy(EntityXfRecordProcessExxx::getDiscount, Collectors.counting()));

        return ResultUtil.success(merchPayCountMap);
    }

    @Override
    public Result discountXfUserparam(Long companyId, EntityXfRecord entityXfRecord, List<EntityXfRecordProcessExxx> processExxxList) {

        Stream<EntityXfRecordProcessExxx> exxxes2 = processExxxList.stream();

//        JoinLambdaWrapper<EntityXfRecordProcessExxx> queryWrapper = new JoinLambdaWrapper<>(EntityXfRecordProcessExxx.class);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPId, entityXfRecord.getPId());
//        queryWrapper.eq(EntityXfRecordProcessExxx::getComId, entityXfRecord.getComId());
//        queryWrapper.in(EntityXfRecordProcessExxx::getConsState, 1, 3, 5);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getPayState, 1);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getIfRefund, 0);
//        queryWrapper.eq(EntityXfRecordProcessExxx::getConsType, entityXfRecord.getConsType());

        if (entityXfRecord.getReserved1() != null && entityXfRecord.getReserved1().equals("selectMerchTrue")) {
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMerchId, entityXfRecord.getMerchId());
            exxxes2 = exxxes2.filter(o -> o.getMerchId().equals(entityXfRecord.getMerchId()));
        }

        String strDate = DateUtils.getStrDate(new Date(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        if (entityXfRecord.getConsTime() != null) {
            strDate = DateUtils.getStrDate(entityXfRecord.getConsTime(), DateUtils.YYYY_MM_DD_HH_MM_SS);
        }

        if (entityXfRecord.getMealNo().equals("500")) {
            //餐段按天
//            queryWrapper.last("and TO_DAYS(create_time) = TO_DAYS('"+strDate+"')");
            strDate = strDate.substring(0, 10);
//            queryWrapper.between( "create_time",strDate+" 00:00:00",strDate+" 23:59:59");
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMealDate, strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        } else if (entityXfRecord.getMealNo().equals("1000")) {
            //按月查询
//            queryWrapper.last("and DATE_FORMAT(create_time,'%Y%m') = DATE_FORMAT('"+strDate+"','%Y%m')");
//            strDate = strDate.substring(0, 7).replace("-", "");
//            queryWrapper.apply(" DATE_FORMAT(meal_date, '%Y%m') =" + strDate);
            strDate = strDate.substring(0, 7);
            String endDate = DateUtils.getLastDayOfMonth(Integer.valueOf(strDate.substring(5)));
//            queryWrapper.between(EntityXfRecordProcessExxx::getMealDate, strDate + "-01", endDate);
            strDate = strDate + "-01";
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate1)) >= 0);
            String finalStrDate2 = endDate;
            exxxes2 = exxxes2.filter(o -> o.getMealDate().compareTo(DateUtils.strToDate2(finalStrDate2)) <= 0);

        } else {
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMealNo, entityXfRecord.getMealNo());
            exxxes2 = exxxes2.filter(o -> o.getMealNo().equals(entityXfRecord.getMealNo()));
            strDate = strDate.substring(0, 10);
//            queryWrapper.eq(EntityXfRecordProcessExxx::getMealDate, strDate);
            String finalStrDate1 = strDate;
            exxxes2 = exxxes2.filter(o -> DateUtil.getDate(o.getMealDate()).equals(finalStrDate1));
        }

//        queryWrapper.notDefaultSelectAll().leftJoin(EntityXfRecordProcessExtend.class, EntityXfRecordProcessExtend::getOrderId, EntityXfRecordProcessExxx::getId)
//                .select(EntityXfRecordProcessExtend::getCancelState).selectAs((cb) -> {
//                    cb.add(" count(*) ","times",false);
//                    cb.add(" sum(real_money) ","amount",false); // 这个为false就是代表不是字符串，会原样查询 在1.3.1版本后支持
//                }).apply("( xf_record_process_extend.cancel_state = 0 or  xf_record_process_extend.cancel_state is null  ) ").end();
//
//        return this.joinGetOne(queryWrapper, TimesAmountVoExxx.class);

        exxxes2 = exxxes2.filter(o -> o.getCancelState() == null || o.getCancelState() == 0);

        LongSummaryStatistics statistics = exxxes2.mapToLong(EntityXfRecordProcessExxx::getRealMoney).summaryStatistics();

        Map<String, Object> map = new HashMap<>();
        map.put("times", statistics.getCount());
        map.put("amount", statistics.getSum());

        return ResultUtil.success(map);

    }
}
